const util = require("util");
const game_config = require("../config/game_config");
const db = require("./Db");
class DbBase extends db{
    constructor() {
        super();

        const config = {
            host: game_config.center_database.host,
            port: game_config.center_database.port,
            user: game_config.center_database.uname,
            password: game_config.center_database.upwd,
            database: game_config.center_database.db_name
        };
        this.myDb = new db(config);
    }
    static getInstance(){
        if(!DbBase.instance){
            DbBase.instance = new DbBase();
            return DbBase.instance;
        }else{
            return DbBase.instance;
        }
    }

    //更新用户球币
    async updateUserBallNum(uid,num){
        let sql = 'update userinfo set ballNum =  \"%d\" where userId = \"%s\"';
        var sql_cmd = util.format(sql, uid,num);
        try {
            await this.myDb.connect();
            const rows = await this.myDb.query(sql_cmd, [1]);
            return rows;
        }catch (e){
            console.log("======updateUserBallNum=catch=======", e);
        } finally {
            await this.myDb.close();
        }
    }

    //更新用户抽奖券
    async updateUserTicketNum(uid,num){
        let sql = 'update userinfo set ticket =  \"%d\" where userId = \"%s\"';
        var sql_cmd = util.format(sql,num,uid);
        try {
            await this.myDb.connect();
            const rows = await this.myDb.query(sql_cmd, [1]);
            return rows;
        }catch (e){
            console.log("======updateUserTicketNum=catch=======", e);
        } finally {
            await this.myDb.close();
        }
    }
}

module.exports = DbBase.getInstance();